/*
 *    This file is part of SSHTunneler.
 *
 *    Copyright 2009 Jean-Sebastien Gelinas <calestar@gmail.com>
 *
 *    SSHTunneler is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    SSHTunneler is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with SSHTunneler.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
package sshtunneler.packager.data;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class ScriptVariables {

	public static ScriptVariables inst = new ScriptVariables();
	private HashMap<String, String> variables = new HashMap<String, String>();
	private String[] raw_automatic_variables = { "USERHOME_DIR", "DESKTOP_DIR" };
	private List<String> automatic_variables = new ArrayList<String>();
	private List<String> variable_names = new ArrayList<String>();

	private ScriptVariables() {
		for (String s : this.raw_automatic_variables) {
			this.automatic_variables.add(s);
			this.variables.put(s, "<<AUTOMATIC>>");
			this.variable_names.add(s);
		}

		this.setVariableValue("INSTALL_DIR", "$(USERHOME_DIR)|sshtunneler");
		this.setVariableValue("LIB_DIR", "$(INSTALL_DIR)|lib");
		this.setVariableValue("TOOLS_DIR", "$(INSTALL_DIR)|tools");
	}

	public boolean is_native(String varname) {
		varname = varname.toUpperCase();
		return this.automatic_variables.contains(varname);
	}

	public boolean changeable(String varname) {
		return !this.is_native(varname);
	}

	public String getVariableValue(String varname) {
		varname = varname.toUpperCase();
		return this.variables.get(varname);
	}

	public boolean hasVariable(String varname) {
		varname = varname.toUpperCase();
		return this.variables.containsKey(varname);
	}

	public void removeVariable(String varname) {
		varname = varname.toUpperCase();
		if (!this.hasVariable(varname)) {
			return;
		}
		this.variables.remove(varname);
		this.variable_names.remove(varname);
	}

	public void setVariableValue(String varname, String value) {
		varname = varname.toUpperCase();
		if (!this.changeable(varname)) {
			return;
		}
		if (!this.hasVariable(varname)) {
			this.variable_names.add(varname);
		}
		this.variables.put(varname, value);
	}

	public List<String> allVariables() {
		return this.variable_names;
	}
}
